export default {
  state: {
    shoppingCart: []
    // shoppingCart结构
    /* 
    shoppingCart = {
      id: this.$store.getters.getUser.username,
        goodsId: this.goodsInfo.goodsId,
        goodsName: this.goodsInfo.goodsName,
        goodsImg: this.goodsInfo.goodsImg, // 商品图片
        goodsPrice: this.goodsInfo.goodsPrice, // 商品价格
        num: this.goodsNum,
        check: false,
    } */
  },
  getters: {
    getShoppingCart(state) {
      // 获取购物车状态
      return state.shoppingCart;
    },
    getNum(state) {
      // 购物车商品总数量
      let totalNum = 0;
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        totalNum += temp.num;
      }
      return totalNum;
    },
    getIsAllCheck(state) {
      // 判断是否全选
      let isAllCheck = true;
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        // 只要有一个商品没有勾选立即return false;
        if (!temp.check) {
          isAllCheck = false;
          return isAllCheck;
        }
      }
      return isAllCheck;
    },
    getCheckGoods(state) {
      // 获取勾选的商品信息
      // 用于确认订单页面
      let checkGoods = [];
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        if (temp.check) {
          checkGoods.push(temp);
        }
      }
      return checkGoods;
    },
    getCheckNum(state) {
      // 获取购物车勾选的商品数量
      let totalNum = 0;
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        if (temp.check) {
          totalNum += temp.num;
        }
      }
      return totalNum;
    },
    getTotalPrice(state) {
      // 购物车勾选的商品总价格
      let totalPrice = 0;
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        if (temp.check) {
          totalPrice += temp.goodsPrice * temp.num;
        }
      }
      return totalPrice;
    },
    getTotalPriceNo(state) {
      let totalPrice = 0;
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        totalPrice += temp.goodsPrice * temp.num;
      }
      return totalPrice;
    },
    getGoodsInfo: (state) => (goodsId) => {
      let goods = state.shoppingCart.find(item => item.goodsId == goodsId)
      if (goods) {
        return goods.num
      } else {
        return 0
      }
    }
  },
  mutations: {
    setShoppingCart(state, data) {
      // 设置购物车状态
      state.shoppingCart = data;
    },
    unshiftShoppingCart(state, data) {
      let goods = state.shoppingCart.find(item => item.goodsId == data.goodsId)
      if (goods) {
        goods.num = data.num
      } else {
        state.shoppingCart.unshift(data);
      }
    },
    updateShoppingCart(state, payload) {
      if (payload.prop == "num") {
        if (payload.val < 1) {
          return;
        }
      }
      state.shoppingCart[payload.key][payload.prop] = payload.val;
    },
    addShoppingCartNum(state, productID) {
      // 增加购物车商品数量
      // 用于在商品详情页点击添加购物车,后台返回002，“该商品已在购物车，数量 +1”，更新vuex的商品数量
      for (let i = 0; i < state.shoppingCart.length; i++) {
        const temp = state.shoppingCart[i];
        if (temp.productID == productID) {
          if (temp.num < temp.maxNum) {
            temp.num++;
          }
        }
      }
    },
    deleteShoppingCart(state, id) {
      // 根据购物车id删除购物车商品
      state.shoppingCart.splice(id, 1);
    },
    checkAll(state, data) {
      // 点击全选按钮，更改每个商品的勾选状态
      for (let i = 0; i < state.shoppingCart.length; i++) {
        state.shoppingCart[i].check = data;
      }
    }
  },
  actions: {
    setShoppingCart({ commit }, data) {
      commit('setShoppingCart', data);
    },
    unshiftShoppingCart({ commit }, data) {
      commit('unshiftShoppingCart', data);
    },
    updateShoppingCart({ commit }, payload) {
      commit('updateShoppingCart', payload);
    },
    addShoppingCartNum({ commit }, productID) {
      commit('addShoppingCartNum', productID);
    },
    deleteShoppingCart({ commit }, id) {
      commit('deleteShoppingCart', id);
    },
    checkAll({ commit }, data) {
      commit('checkAll', data);
    }
  }
}